package zw_901_1000.zw_977_有序数组的平方;

import java.util.Arrays;

class Solution {
    public static void main(String[] args) {
        int[] nums = {-4, -1, 0, 3, 10};
        int[] ints = sortedSquares(nums);
        for (int anInt : ints) {
            System.out.print(anInt + " ");
        }
    }

    public static int[] sortedSquares(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n];
        for (int i = 0, j = n - 1, pos = n - 1; i <= j; ) {
            if (nums[i] * nums[i] > nums[j] * nums[j]) {
                ans[pos] = nums[i] * nums[i];
                i++;
            } else {
                ans[pos] = nums[j] * nums[j];
                j--;
            }
            pos--;
        }
        return ans;
    }


//    public static int[] sortedSquares(int[] nums) {
//        int[] ans = new int[nums.length];
//        for (int i = 0; i < nums.length; i++) {
//            ans[i] = nums[i] * nums[i];
//        }
//        Arrays.sort(ans);
//        return ans;
//    }
}
